Implementing User Authentication
Table of Contents
Implementing Registration
Log-in presupposes a collection of valid usernames and passwords in the database. One way to get those combinations into the database is to create a registration form that inserts them there. I have created the form for you: It's in the exercise files, called registration.asp, registration.cfm, or registration.php (henceforth, I won't spell out all the extensions, but will only refer to ASP).
-
Open registration.asp in Dreamweaver.
I have already created the web form.
-
Take a moment to click each of the form elements and find out their names in the Property inspector.
Each item entered into a form field is stored as a variable using the name of the field. The first form field is the firstName field. If you type Jeffrey into the field and submit the form, the page submits a piece of data,
firstName=Jeffrey. -
Click the hidden form field (the yellow icon) in the lower-left corner of the form. In the Property inspector, name it userGroup and give it a value of visitor.
Figure 2. The hidden form field as shown in the Property inspector
Sometimes, you will need to pass data in a form that you don't want to collect from—or even provide access to—the user. This is one such situation. This user authentication framework will use roles-based authorization. Specifically, any registered user will be able to access some, but not all of the pages in the site. Some of the pages are restricted so that only special registered users can access them (think of admin pages, for example). One of the fields in the users table of the database is userGroup, which specifies whether the user has basic or full access to the site.
Obviously, you don't want to let the users determine whether they should have basic or full access. By creating this hidden field, you assign everyone to the basic group by default. An administrator will have to manually upgrade a given user's status, if she should be in the full-access group.
The form is now ready; it's time to add the script.
-
Click the Submit button. In the Server Behaviors panel, click the New Server Behavior (+) button, and choose Insert Record.
The Insert Record dialog box appears.
The Insert Record server behavior enables you to capture data from a web form and insert it as a new record in a given database table.
-
In the Insert Record dialog box, choose user_auth as the Connection (or whatever you named your database connection). Verify that you selected tbl_users in the Insert Into Table field. In the After Inserting Go To field, enter login.asp. Accept the rest of the defaults and click OK.
Figure 3. The Insert Record dialog box
The lower half of the dialog box enables you to specify which form data your application should insert into which database field; it is like a matching game. For example, you want the data from the firstName web form field to go into the firstName column in the database. If—as is the case here—you name the form fields the same as the database fields, Dreamweaver guesses by default that they are a match, and therefore, you don't have to manually specify each one.
With this server behavior in place, you have made it possible for users to create new profiles. However, you have not taken into account that two users might create profiles under the same username; you want to make this impossible.
-
Click to select the Submit button again. In the Server Behaviors panel, click the New Server Behavior (+) button, and choose User Authentication > Check New Username.
The Check New Username dialog box appears.
-
In the Check New Username dialog box, select username (or FORM.username, as ColdFusion is likely to display it) from the Username field pop-up menu and type registration_failed.asp in the If Already Exists, Go To field. Click OK.
Figure 4. The Check New Username dialog box
Since you asked for the user's e-mail address to use as their username, this shouldn't be a problem for legitimate users.
Whenever you create a form, make sure you deploy some sort of form validation, whether it's a client-side JavaScript behavior or a server-side script. In this case, I have already added a Validate Form behavior (a regular JavaScript behavior found in the Behaviors, not Server Behaviors, panel) to fulfill this function.
-
Save and if necessary, upload, register.asp. Close the file.
If you like, you can test this component of the site now or you can wait until you have added the login component.